//
//  SettingViewController2.m
//  faceDemo
//
//  Created by golang on 2022/3/30.
//  Copyright © 2022 hisign. All rights reserved.
//

#import "SettingViewController.h"
#import <HSFaceDetector/LiveHeader.h>

#define LIVE_IP @"http://lic.biometric.net.cn:11080"

@interface SettingViewController ()<UITextFieldDelegate>
@property(nonatomic, assign)CGFloat keyboardHeight;
@property(nonatomic, weak)UIView *containsView;
@property(nonatomic, weak)UITextField *IPTextField;
@property(nonatomic, weak)UITextField *timeoutField;
@property(nonatomic, weak)UITextField *minFaceSizeField;
@property(nonatomic, weak)UITextField *maxFaceSizeField;
@property(nonatomic, weak)UIButton *silentSelect;
@property(nonatomic, weak)UIButton *actionSelect;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.keyboardHeight = 256;
    [self initView];
    //增加监听，当键盘出现或改变时获取键盘高度
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

- (void)keyboardWillShow:(NSNotification *)aNotification{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    self.keyboardHeight = keyboardRect.size.height;
}

- (void)initView{
    
    UIView *containsView = [[UIView alloc]initWithFrame:self.view.bounds];
    containsView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:containsView];
    self.containsView = containsView;
    
    UIButton *quitBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 44, 44, 44)];
    [quitBtn setImage:[UIImage imageNamed:@"HSFaceData.bundle/image/face_cancel.png"] forState:UIControlStateNormal];
    [quitBtn addTarget:self action:@selector(quit) forControlEvents:UIControlEventTouchUpInside];
    [containsView addSubview:quitBtn];
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 44, SCREENWIDTH, 44)];
    titleLab.text = @"活体检测配置";
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont boldSystemFontOfSize:22];
    titleLab.textColor = [UIColor blackColor];
    [containsView addSubview:titleLab];
    
    UITextField *IPTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(titleLab.frame) + 64, SCREENWIDTH - 60, 44)];
    IPTextField.backgroundColor = [UIColor whiteColor];
    IPTextField.textAlignment = NSTextAlignmentCenter;
    IPTextField.keyboardType = UIKeyboardTypeURL;
    IPTextField.placeholder = @"请输入云端服务地址";
    IPTextField.textColor = HSBaseColor;
    IPTextField.layer.borderWidth = 1;
    IPTextField.layer.borderColor = [UIColor grayColor].CGColor;
//    IPTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
//    IPTextField.leftViewMode = UITextFieldViewModeAlways;
    [containsView addSubview:IPTextField];
    self.IPTextField = IPTextField;
    NSString *ipString = [[NSUserDefaults standardUserDefaults]objectForKey:@"live_ip"];
    if (ipString != nil && ipString.length != 0) {
        self.IPTextField.text = ipString;
    }else {
        self.IPTextField.text = LIVE_IP;
    }
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(IPTextField.frame)+15, SCREENWIDTH, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    [containsView addSubview:lineView];
    
    UILabel *liveTypeTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(lineView.frame)+15, 180, 44)];
    liveTypeTitle.text = @"选择活体模式";
    liveTypeTitle.textAlignment = NSTextAlignmentLeft;
    liveTypeTitle.font = [UIFont systemFontOfSize:20];
    liveTypeTitle.textColor = [UIColor blackColor];
    [containsView addSubview:liveTypeTitle];
    
    CGFloat viewW = 120;
    CGFloat viewH = 20;
    CGFloat marginX = (SCREENWIDTH - 2 * viewW)/3;
    CGFloat marginY = CGRectGetMaxY(liveTypeTitle.frame)+15;
    UIButton *silentSelect = [[UIButton alloc]initWithFrame:CGRectMake(marginX, marginY, viewW, viewH)];
    [silentSelect setImage:[UIImage imageNamed:@"HSFaceData.bundle/image/optionBtn.png"] forState:UIControlStateNormal];
    [silentSelect setImage:[UIImage imageNamed:@"HSFaceData.bundle/image/selectedBtn.png"] forState:UIControlStateSelected];
    [silentSelect setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, viewW - viewH)];
    [silentSelect setTitle:@"静默活体" forState:UIControlStateNormal];
    [silentSelect setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    silentSelect.selected = [[NSUserDefaults standardUserDefaults]integerForKey:@"live_mode"] == 1;
    [silentSelect addTarget:self action:@selector(selectSilentLive:) forControlEvents:UIControlEventTouchUpInside];
    [containsView addSubview:silentSelect];
    self.silentSelect = silentSelect;
    
    UIButton *actionSelect = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(silentSelect.frame)+marginX, marginY, viewW, viewH)];
    [actionSelect setImage:[UIImage imageNamed:@"HSFaceData.bundle/image/optionBtn.png"] forState:UIControlStateNormal];
    [actionSelect setImage:[UIImage imageNamed:@"HSFaceData.bundle/image/selectedBtn.png"] forState:UIControlStateSelected];
    [actionSelect setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, viewW - viewH)];
    [actionSelect setTitle:@"动作活体" forState:UIControlStateNormal];
    actionSelect.selected = [[NSUserDefaults standardUserDefaults]integerForKey:@"live_mode"] == 0;
    [actionSelect setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [actionSelect addTarget:self action:@selector(selectActionLive:) forControlEvents:UIControlEventTouchUpInside];
    [containsView addSubview:actionSelect];
    self.actionSelect = actionSelect;
    
    CGFloat viewY = CGRectGetMaxY(actionSelect.frame)+15;
    
    UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(0, viewY, SCREENWIDTH, 1)];
    lineView1.backgroundColor = [UIColor blackColor];
    [containsView addSubview:lineView1];
    
    UILabel *timeTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(lineView1.frame)+15, 100, 44)];
    timeTitle.text = @"超时时间:";
    timeTitle.textAlignment = NSTextAlignmentLeft;
    timeTitle.font = [UIFont systemFontOfSize:20];
    timeTitle.textColor = [UIColor blackColor];
    [containsView addSubview:timeTitle];
    
    UITextField *timeField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(timeTitle.frame), CGRectGetMaxY(lineView1.frame)+15, 60, 44)];
    timeField.keyboardType = UIKeyboardTypeNumberPad;
    timeField.placeholder = @"请输入";
    timeField.textColor = [UIColor blackColor];
    timeField.delegate = self;
    NSString *timeout = [[NSUserDefaults standardUserDefaults]objectForKey:@"time_out_second"];
    timeField.text = timeout ? timeout : @"8";
    [containsView addSubview:timeField];
    self.timeoutField = timeField;
    
    UIView *lineView4 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(timeTitle.frame)+15, SCREENWIDTH, 1)];
    lineView4.backgroundColor = [UIColor blackColor];
    [containsView addSubview:lineView4];
    
    UILabel *minFaceTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(lineView4.frame)+15, 100, 44)];
    minFaceTitle.text = @"最小人脸:";
    minFaceTitle.textAlignment = NSTextAlignmentLeft;
    minFaceTitle.font = [UIFont systemFontOfSize:20];
    minFaceTitle.textColor = [UIColor blackColor];
    [containsView addSubview:minFaceTitle];
    
    UITextField *minFaceField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(minFaceTitle.frame), CGRectGetMaxY(lineView4.frame)+15, 60, 44)];
    minFaceField.keyboardType = UIKeyboardTypeNumberPad;
    minFaceField.placeholder = @"请输入";
    minFaceField.textColor = [UIColor blackColor];
    minFaceField.delegate = self;
    NSString *minFaceSize = [[NSUserDefaults standardUserDefaults]objectForKey:@"min_face_size"];
    minFaceField.text = minFaceSize ? minFaceSize : @"80";
    [containsView addSubview:minFaceField];
    self.minFaceSizeField = minFaceField;
    
    UIView *lineView5 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(minFaceTitle.frame)+15, SCREENWIDTH, 1)];
    lineView5.backgroundColor = [UIColor blackColor];
    [containsView addSubview:lineView5];
    
    UILabel *maxFaceTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(lineView5.frame)+15, 100, 44)];
    maxFaceTitle.text = @"最大人脸:";
    maxFaceTitle.textAlignment = NSTextAlignmentLeft;
    maxFaceTitle.font = [UIFont systemFontOfSize:20];
    maxFaceTitle.textColor = [UIColor blackColor];
    [containsView addSubview:maxFaceTitle];
    
    UITextField *maxFaceField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(maxFaceTitle.frame), CGRectGetMaxY(lineView5.frame)+15, 60, 44)];
    maxFaceField.keyboardType = UIKeyboardTypeNumberPad;
    maxFaceField.placeholder = @"请输入";
    maxFaceField.textColor = [UIColor blackColor];
    maxFaceField.delegate = self;
    NSString *maxFaceSize = [[NSUserDefaults standardUserDefaults]objectForKey:@"max_face_size"];
    maxFaceField.text = maxFaceSize ? maxFaceSize : @"160";
    [containsView addSubview:maxFaceField];
    self.maxFaceSizeField = maxFaceField;
    
    UIView *lineView6 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(maxFaceTitle.frame)+15, SCREENWIDTH, 1)];
    lineView6.backgroundColor = [UIColor blackColor];
    [containsView addSubview:lineView6];

}

- (void)quit{
    [self.view endEditing:YES];
    
    [[NSUserDefaults standardUserDefaults]setObject:self.IPTextField.text forKey:@"live_ip"];
    [[NSUserDefaults standardUserDefaults]setObject:self.timeoutField.text forKey:@"time_out_second"];
    [[NSUserDefaults standardUserDefaults]setObject:self.minFaceSizeField.text forKey:@"min_face_size"];
    [[NSUserDefaults standardUserDefaults]setObject:self.maxFaceSizeField.text forKey:@"max_face_size"];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)selectSilentLive:(UIButton *)btn{
    btn.selected = !btn.selected;
    [[NSUserDefaults standardUserDefaults]setInteger:1 forKey:@"live_mode"];
    if (btn.selected) {
        self.actionSelect.selected = !btn.selected;
    }
}

- (void)selectActionLive:(UIButton *)btn{
    btn.selected = !btn.selected;
    [[NSUserDefaults standardUserDefaults]setInteger:0 forKey:@"live_mode"];
    if (btn.selected) {
        self.silentSelect.selected = !btn.selected;
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    CGFloat h = SCREENHEIGHT - HomeBarPadding - CGRectGetMaxY(textField.frame);
    NSLog(@"keyboardHeight = %f, h = %f", self.keyboardHeight, h);
    if (h < self.keyboardHeight) {
        NSLog(@"需要滚动向上移动");
        [UIView animateWithDuration:0.5 animations:^{
            CGRect frame = self.containsView.frame;
            frame.origin.y = h - self.keyboardHeight;
            self.containsView.frame = frame;
        }];
        
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = self.containsView.frame;
        frame.origin.y = 0;
        self.containsView.frame = frame;
    }];
    
}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
